define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'certification/user_shop/index' + location.search,
                    add_url: 'certification/user_shop/add',
                    edit_url: 'certification/user_shop/edit',
                    // del_url: 'certification/user_shop/del',
                    multi_url: 'certification/user_shop/multi',
                    import_url: 'certification/user_shop/import',
                    deal_url:  'order/order/index',
                    table: 'user_shop',
                    dragsort_url: false
                }
            });

            var table = $("#table");

            table.on("post-common-search.bs.table", function (e, t) {
                // 这个代码块中的操作自行调整
                var link_obj = $("ul.nav-tabs li a[data-toggle='tab']").first();
                link_obj.parent("li").addClass("active");
                $("select[name='status']", t.$commonsearch).val(link_obj.data("value"));
                $("li[role='menuitem']:has(input[data-field='status'])").hide();
                $('label[for="status"]').parent().css('display','none');
                $("#table").bootstrapTable('hideColumn','status');
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                commonSearch: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false,visible:false},
                        {field: 'create_time', title: __('注册时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user_name', title: __('User_name'), operate: 'LIKE'},
                        {field: 'province', title: __('Province'), operate: 'LIKE'},
                        {field: 'city', title: __('City'), operate: 'LIKE'},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'price_sum', title: __('Price_sum'), operate:false},
                        {field: 'trading_sum', title: __('Trading_sum'),operate:false},
                        {field: 'goods_num', title: __('Goods_num'),operate:false},
                        {field: 'credit_score', title: __('Credit_score'), operate: false},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE',visible:false},
                        {field: 'blackwitch', title: __('Blackwitch'), operate: false, searchList: {"1": '加入黑名单', "0": '移除黑名单'},  table: table, formatter: Table.api.formatter.toggle},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2')},
                            visible: false,
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            width:'100px',
                            cellStyle: function(){
                                return { css: {
                                        "max-width" : "100px",
                                        "text-overflow" : "ellipsis",
                                        "white-space" : "initial",
                                    }}
                            },
                            buttons: [
                                {
                                    name : 'edit',
                                    text : '审核',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    url : $.fn.bootstrapTable.defaults.extend.edit_url,
                                    refresh: true,
                                    visible: function(row){
                                        if (row.status == 1)
                                        {
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'show',
                                    text: '详情',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'certification/user_shop/show',
                                    refresh: true,
                                    visible: function (row){
                                        if (row.status == 0)
                                        {
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'deal',
                                    text: '交易记录',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url : $.fn.bootstrapTable.defaults.extend.deal_url + '?uid={uid}',
                                    visible : true
                                }

                            ],
                            formatter: Table.api.formatter.operate
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        show: function () {
            Controller.api.bindevent();
        },
        api: {

            bindevent: function () {
                $(".layer-close").click(function () {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
